package astudy.antlr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventManager {

	List<String> patterns;
	Map<String, List<ASTListener>> mapListeners;
	
	public EventManager() {
		
		patterns = new ArrayList<String>();
		mapListeners = new HashMap<String, List<ASTListener>>();
	}
	
	public void append(String pattern, ASTListener listener) {
		
		List<ASTListener> listeners = this.mapListeners.get(pattern);
		if (listeners == null) {
			listeners = new ArrayList<ASTListener>();
			patterns.add(pattern);
		}
		listeners.add(listener);
		mapListeners.put(pattern, listeners);
	}
	
	public List<ASTListener> getListeners(String pattern) {
		
		List<ASTListener> listeners = new ArrayList<ASTListener>();
		for (String p: patterns) {
			if (p.equalsIgnoreCase(pattern)) {
				listeners.addAll(this.mapListeners.get(p));
			}
		}
		listeners.addAll(this.mapListeners.get("QueryNode"));
		return listeners;
	}
}
